﻿using ProjectManager.Interface;
using ProjectManager.Logic.Content;
using ProjectManager.MainFormTools;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManager.Controller
{
    internal class UserController : IUser
    {
        private bool inputValid;
        private ILogic logicController;
        public WaitingForm waitingForm;

        public UserController(ILogic logicController)
        {
            this.logicController = logicController;
            waitingForm = new WaitingForm();
            WaitingForm.CheckForIllegalCrossThreadCalls = false;
        }
        public void EndWait()
        {
            waitingForm.Hide();
        }

        public void GetInput(string title, List<string> rowTitle, List<Type> rowType, out List<object> rowInput)
        {
            inputValid = false;

            int count = rowTitle.Count;
            Form GetInputForm = new Form();
            GetInputForm.Text = title;
            GetInputForm.Size = new System.Drawing.Size(600, 100 + 50 * count > 200 ? 100 + 50 * count+200:400);
            UIMarkLabel[] labels = new UIMarkLabel[count];
            //UITextBox[] textBoxes = new UITextBox[count];
            UISymbolButton confirmButton = new UISymbolButton();
            //ComboBox[] comboBoxs= new ComboBox[count];
            Control[] controls = new Control[count];
            List<string> paths = new List<string>();

            rowInput = new List<object>();
            for (int i = 0; i < count; i++)
            {

                if (rowType[i] == typeof(bool))
                {
                    labels[i] = new UIMarkLabel();
                    labels[i].Location = new System.Drawing.Point(50, 50 + 50 * i);
                    labels[i].Text = rowTitle[i];
                    labels[i].AutoSize = true;
                    GetInputForm.Controls.Add(labels[i]);

                    controls[i] = new UIComboBox();
                    controls[i].Location = new System.Drawing.Point(250, 50 + 50 * i);
                    controls[i].Size = new System.Drawing.Size(200, 20);
                    ((UIComboBox)controls[i]).Items.Add("是");
                    ((UIComboBox)controls[i]).Items.Add("否");
                    ((UIComboBox)controls[i]).SelectedIndex = -1;
                    controls[i].Text = "请选择";
                    controls[i].ForeColor = System.Drawing.Color.Gray;
                    ((UIComboBox)controls[i]).SelectedIndexChanged += (sender, e) =>
                    {
                        if (((UIComboBox)sender).SelectedIndex == -1)
                        {
                            ((UIComboBox)sender).Text = "请输入";
                            ((UIComboBox)sender).ForeColor = System.Drawing.Color.Gray;
                        }
                        else
                        {
                            ((UIComboBox)sender).ForeColor = System.Drawing.Color.Black;
                        }
                    };

                    GetInputForm.Controls.Add(controls[i]);
                    rowInput.Add(((UIComboBox)controls[i]).SelectedIndex == 0 ? true : false);
                }
                else if (rowType[i] == typeof(string))
                {
                    labels[i] = new UIMarkLabel();
                    labels[i].Location = new System.Drawing.Point(50, 50 + 50 * i);
                    labels[i].Text = rowTitle[i];
                    labels[i].AutoSize = true;
                    GetInputForm.Controls.Add(labels[i]);

                    controls[i] = new UITextBox();
                    controls[i].Location = new System.Drawing.Point(250, 50 + 50 * i);
                    controls[i].Size = new System.Drawing.Size(200, 20);
                    controls[i].Text = "请输入";
                    controls[i].ForeColor = System.Drawing.Color.Gray;
                    ((UITextBox)controls[i]).Enter += (sender, e) =>
                    {
                        if (((UITextBox)sender).Text == "请输入")
                        {
                            ((UITextBox)sender).Text = "";
                            ((UITextBox)sender).ForeColor = System.Drawing.Color.Black;
                        }
                    };
                    ((UITextBox)controls[i]).Leave += (sender, e) =>
                    {
                        if (string.IsNullOrWhiteSpace(((UITextBox)sender).Text))
                        {
                            ((UITextBox)sender).Text = "请输入";
                            ((UITextBox)sender).ForeColor = System.Drawing.Color.Gray;
                        }
                    };
                    GetInputForm.Controls.Add(controls[i]);
                    rowInput.Add(((UITextBox)controls[i]).Text);
                }
                else if (rowType[i] == typeof(DateTime))
                {
                    labels[i] = new UIMarkLabel();
                    labels[i].Location = new System.Drawing.Point(50, 50 + 50 * i);
                    labels[i].Text = rowTitle[i];
                    labels[i].AutoSize = true;
                    GetInputForm.Controls.Add(labels[i]);

                    controls[i] = new UIDatePicker();
                    controls[i].Location = new System.Drawing.Point(250, 50 + 50 * i);
                    controls[i].Size = new System.Drawing.Size(200, 20);
                    GetInputForm.Controls.Add(controls[i]);
                    rowInput.Add(((UIDatePicker)controls[i]).Value.Date);
                }
                else if (rowType[i] == typeof(List<string>))
                {
                    labels[i] = new UIMarkLabel();
                    labels[i].Location = new System.Drawing.Point(50, 50 + 50 * i);
                    labels[i].Text = rowTitle[i];
                    labels[i].AutoSize = true;
                    GetInputForm.Controls.Add(labels[i]);

                    controls[i] = new UIRichTextBox();
                    controls[i].Location = new System.Drawing.Point(250, 50 + 50 * i);
                    controls[i].Size = new System.Drawing.Size(200, 40);
                    ((UIRichTextBox)controls[i]).Text = "每行输入一个路径";
                    ((UIRichTextBox)controls[i]).ForeColor = System.Drawing.Color.Gray;
                    ((UIRichTextBox)controls[i]).Enter += (sender, e) =>
                    {
                        if (((UIRichTextBox)sender).Text == "每行输入一个路径")
                        {
                            ((UIRichTextBox)sender).Text = "";
                            ((UIRichTextBox)sender).ForeColor = System.Drawing.Color.Black;
                        }
                    };
                    ((UIRichTextBox)controls[i]).Leave += (sender, e) =>
                    {
                        if (string.IsNullOrWhiteSpace(((UIRichTextBox)sender).Text))
                        {
                            ((UIRichTextBox)sender).Text = "每行输入一个路径";
                            ((UIRichTextBox)sender).ForeColor = System.Drawing.Color.Gray;
                        }
                    };
                    GetInputForm.Controls.Add(controls[i]);
                    rowInput.Add(paths);
                }
                else if (rowType[i] == typeof(ProjectTaskStatus))
                {
                    labels[i] = new UIMarkLabel();
                    labels[i].Location = new System.Drawing.Point(50, 50 + 50 * i);
                    labels[i].Text = rowTitle[i];
                    labels[i].AutoSize = true;
                    GetInputForm.Controls.Add(labels[i]);

                    controls[i] = new UIComboBox();
                    controls[i].Location = new System.Drawing.Point(250, 50 + 50 * i);
                    controls[i].Size = new System.Drawing.Size(200, 20);
                    ((UIComboBox)controls[i]).Items.Add("Working");
                    ((UIComboBox)controls[i]).Items.Add("WaitForAudit");
                    ((UIComboBox)controls[i]).Items.Add("Succeeded");
                    ((UIComboBox)controls[i]).Items.Add("Stopped");
                    ((UIComboBox)controls[i]).Items.Add("Failed");
                    ((UIComboBox)controls[i]).SelectedIndex = -1;
                    controls[i].Text = "请选择";
                    controls[i].ForeColor = System.Drawing.Color.Gray;
                    ((UIComboBox)controls[i]).SelectedIndexChanged += (sender, e) =>
                    {
                        if (((UIComboBox)sender).SelectedIndex == -1)
                        {
                            ((UIComboBox)sender).Text = "请输入";
                            ((UIComboBox)sender).ForeColor = System.Drawing.Color.Gray;
                        }
                        else
                        {
                            ((UIComboBox)sender).ForeColor = System.Drawing.Color.Black;
                        }
                    };
                    GetInputForm.Controls.Add(controls[i]);
                    rowInput.Add((ProjectTaskStatus)((UIComboBox)controls[i]).SelectedIndex);
                }
                else if (rowType[i] == typeof(TeamInformation))
                {
                    labels[i] = new UIMarkLabel();
                    labels[i].Location = new System.Drawing.Point(50, 50 + 50 * i);
                    labels[i].Text = rowTitle[i];
                    labels[i].AutoSize = true;
                    GetInputForm.Controls.Add(labels[i]);

                    controls[i] = new UIComboBox();
                    controls[i].Location = new System.Drawing.Point(250, 50 + 50 * i);
                    controls[i].Size = new System.Drawing.Size(200, 20);
                    ((UIComboBox)controls[i]).DataSource = logicController.GetTeams();
                    ((UIComboBox)controls[i]).DisplayMember = "Name";
                  
                    GetInputForm.Controls.Add(controls[i]);
                    rowInput.Add((TeamInformation)(((UIComboBox)controls[i]).SelectedItem));
                }
                else
                {
                    throw new Exception("未知的输入类型");
                }

            }

            confirmButton.Location = new System.Drawing.Point(200, 50 + 50 * count>300?50+50*count:300);
            confirmButton.Text = "确认";
            confirmButton.Size = new System.Drawing.Size(100, 30);
            GetInputForm.Controls.Add(confirmButton);

            
            confirmButton.Click += (sender, e) =>
            {
                for (int i = 0; i < controls.Length; i++)
                {
                    if (controls[i] is UITextBox)
                    {
                        if (((UITextBox)controls[i]).Text == "请输入")
                        {
                            ShowMessage("请输入" + labels[i].Text);
                            return;
                        }
                    }
                    else if (controls[i] is UIComboBox)
                    {
                        if (((UIComboBox)controls[i]).SelectedIndex == -1)
                        {
                            ShowMessage("请选择" + labels[i].Text);
                            return;
                        }
                    }
                    else if (controls[i] is UIDatePicker)
                    {
                        //
                    }
                    else if (controls[i] is UIRichTextBox)
                    {
                        if (((UIRichTextBox)controls[i]).Text == "每行输入一个路径")
                        {
                            ShowMessage("请输入" + labels[i].Text);
                            return;
                        }
                        else
                        {
                            paths = new List<string>(((UIRichTextBox)controls[i]).Text.Split('\n'));
                            paths.RemoveAll(x => string.IsNullOrWhiteSpace(x));
                        }
                    }
                }
                inputValid = true;
                GetInputForm.Close();
            };
            GetInputForm.ShowDialog();

            for(int i = 0;i< controls.Length; i++)
            {
                if (controls[i] is UITextBox)
                {
                    rowInput[i] = ((UITextBox)controls[i]).Text;
                }
                else if (controls[i] is UIComboBox)
                {
                    if (rowType[i] == typeof(bool))
                    {
                        rowInput[i] = ((UIComboBox)controls[i]).SelectedIndex ==0 ? true : false;
                    }
                    else if (rowType[i] == typeof(ProjectTaskStatus))
                    {
                        rowInput[i] = (ProjectTaskStatus)((UIComboBox)controls[i]).SelectedIndex;
                    }
                    else if (rowType[i] == typeof(TeamInformation))
                    {
                        rowInput[i] = (TeamInformation)(((UIComboBox)controls[i]).SelectedItem);
                    }
                    else
                    {
                        rowInput[i] = ((UIComboBox)controls[i]).SelectedText;
                    }
                }
                else if (controls[i] is UIDatePicker)
                {
                    rowInput[i] = ((UIDatePicker)controls[i]).Value.Date;
                }
                else if (controls[i] is UIRichTextBox)
                {
                    rowInput[i] = paths;
                }
            }

        }
        public bool GetInputValid()
        {
            return inputValid;
        }

        public void ShowErrorMessage(string message)
        {
            UIMessageBox.ShowError2(message);
        }

        public void ShowMessage(string message)
        {
            UIMessageBox.Show(message);
        }

        public void ShowWait(string message)
        {
            waitingForm.descriptionLabel.Text = message;
            waitingForm.TopLevel = true;
            waitingForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            waitingForm.Show();
            // waitingForm.ShowDialog();
        }
    }
}
